package org.jeecg.modules.bill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.bill.entity.MedicineSell;
import org.jeecg.modules.bill.mapper.MedicineSellMapper;
import org.jeecg.modules.bill.service.MedicineSellService;
import org.jeecg.modules.stock.entity.MedicineStock;
import org.jeecg.modules.stock.mapper.MedicineStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 药品出库售货表
 * @Author: 孔小波
 * @Date:   2020-03-24
 * @Version: V1.0
 */
@Service
public class MedicineSellServiceImpl extends ServiceImpl<MedicineSellMapper, MedicineSell> implements MedicineSellService {

    @Autowired
    private MedicineSellMapper medicineSellMapper;
    @Autowired
    private MedicineStockMapper medicineStockMapper;

    @Override
    @Transactional
    public void saveMedicineSell(MedicineSell medicineSell) {
        medicineSellMapper.insert(medicineSell);
        //修改库存信息
        MedicineStock medicineStock=medicineStockMapper.selectById(medicineSell.getStockid());
        //判断库存
        Integer stocks=medicineStock.getStocks();//库存数量
        Integer numbers=medicineSell.getNumbers();//出货数量
        Integer sum=stocks-numbers;
        if(sum==0){
            medicineStockMapper.deleteById(medicineStock.getId());
        }else {
            medicineStock.setStocks(stocks-numbers);
            medicineStockMapper.updateById(medicineStock);
        }
    }
}
